var http = require('http');
var url = require('url');
var util = require('util');

var requestAd = require('./models/requestAd/requestAd.js');


http.createServer(function(req, res){

    res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
    // 解析URL
    //console.log(util.inspect(url.parse(req.url, true)))
    switch (url.parse(req.url, true).pathname)
    {
        // 广告转发的场合
        case '/requestAd':
            requestAd.select (req, cb);
            break;
        default :
            res.end(
                JSON.stringify(
                    {
                        code: 0,
                        err: '无法找到相应API'
                    }
                )
            );

    }

    function cb(param) {
        res.end(
            JSON.stringify(
                param
            )
        );
    }

}).listen(3000);
